package adapters;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.boboqi.test.R;

import java.util.ArrayList;
import java.util.List;

import fragments.PageFragment;

/**
 * Created by pc on 2017/3/2.
 */

public class FragmentTabLayoutPageAdapter extends FragmentPagerAdapter {

    private Context context;

    public FragmentTabLayoutPageAdapter(FragmentManager fm, Context context) {
        super(fm);
        this.context = context;
    }

    private List<String> list = new ArrayList<>();

    public void setData(List<String> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public Fragment getItem(int position) {
        PageFragment fragment = new PageFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("index", position + 1);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return list.get(position);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    public View getTabView(int position) {
        View view = LayoutInflater.from(context).inflate(R.layout.tablayout_tab_item, null);
        TextView tv = (TextView) view.findViewById(R.id.textView);
        tv.setText(list.get(position));
        return view;
    }
}
